package com.sskj.one.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.one.R;
import com.sskj.one.WebActivity;
import com.sskj.one.bean.AccountBean;
import com.sskj.one.util.EditUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/21
 */
public class RegisterActivity extends BaseActivity<RegisterPresenter> {

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_see)
    ImageView ivSee;
    @BindView(R.id.et_pwd_again)
    EditText etPwdAgain;
    @BindView(R.id.iv_see_again)
    ImageView ivSeeAgain;
    @BindView(R.id.et_invite)
    EditText etInvite;
    @BindView(R.id.register_rules)
    CheckBox registerRules;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.to_login)
    ConstraintLayout toLogin;
    @BindView(R.id.iv_close)
    ImageView ivClose;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_register;
    }

    @Override
    public RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        SpannableString spannableString = new SpannableString("提交注册即认为同意《用户注册协议》");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(ContextCompat.getColor(RegisterActivity.this, R.color.theme));
            }

            @Override
            public void onClick(@NonNull View widget) {
                WebActivity.start(RegisterActivity.this, "register_agree");
            }
        };
        spannableString.setSpan(clickableSpan, spannableString.toString().indexOf("《"), spannableString.toString().indexOf("》")+1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        registerRules.setText(spannableString);
        registerRules.setMovementMethod(new LinkMovementMethod());

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(300, ivSee, () -> {
            EditUtil.togglePs(etPwd, ivSee);
        });
        ClickUtil.click(300, ivSeeAgain, () -> {
            EditUtil.togglePs(etPwdAgain, ivSeeAgain);
        });
        ClickUtil.click(300, tvGetCode, () -> {
            if (isEmpty(etMobile)) {
                toastHint(etMobile);
                return;
            }
            if (!CheckUtil.isMobile(getText(etMobile))) {
                ToastUtils.show("手机号格式错误");
                return;
            }
            mPresenter.getSMSCode(getText(etMobile));
        });
        ClickUtil.click(300, btnRegister, () -> {
            if (isEmpty(etMobile)) {
                toastHint(etMobile);
                return;
            }
            if (!CheckUtil.isMobile(getText(etMobile))) {
                ToastUtils.show("手机号格式错误");
                return;
            }
            if (isEmpty(etCode)) {
                toastHint(etCode);
                return;
            }
            if (isEmpty(etPwd)) {
                toastHint(etPwd);
                return;
            }
            if (!CheckUtil.isPayPwd(getText(etPwd))) {
                toastHint(etPwd);
                return;
            }
            if (isEmpty(etPwdAgain)) {
                toastHint(etPwdAgain);
                return;
            }
            if (!getText(etPwd).equals(getText(etPwdAgain))) {
                ToastUtils.show("两次密码输入不一致");
                return;
            }
            if (isEmpty(etInvite)) {
                ToastUtils.show("请输入邀请码");
                return;
            }
            if (!registerRules.isChecked()) {
                ToastUtils.show("请先阅读并同意《用户注册协议》");
                return;
            }
            mPresenter.register(getText(etMobile)
                    , getText(etCode)
                    , getText(etPwd)
                    , getText(etPwdAgain)
                    , getText(etInvite));
        });
        ClickUtil.click(300, toLogin, this::finish);
        ClickUtil.click(300, ivClose, this::finish);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    public void getCodeOK() {
        startTimeDown(tvGetCode);
    }

    public void registerOK(AccountBean data) {
        if (data == null) {
            return;
        }
        ToastUtils.show("注册成功");
        finish();
    }
}
